SlideShare a Scribd company logo
Class No.13  Data Structures http://ecomputernotes.com
Trace of insert 17, 9, 14, 5 14 15 4 9 7 18 3 5 16 20 17 p p->setRight( node ); node http://ecomputernotes.com
Cost of Search Given that a binary tree is level  d  deep. How long does it take to find out whether a number is already present? Consider the insert(17) in the example tree. Each time around the while loop, we did one comparison. After the comparison, we moved a level  down . http://ecomputernotes.com
Cost of Search With the binary tree in place, we can write a routine  find(x)  that returns true if the number  x  is present in the tree, false otherwise. How many comparison are needed to find out if  x  is present in the tree? We do one comparison at each level of the tree until either  x  is found or q becomes NULL. http://ecomputernotes.com
Cost of Search If the binary tree is built out of  n  numbers, how many comparisons are needed to find out if a number  x  is in the tree? Recall that the depth of the complete binary tree built using ‘ n ’ nodes will be  log 2 (n+1) – 1. For example, for n=100,000, log 2 (100001) is less than 20; the tree would be 20 levels deep. http://ecomputernotes.com
Cost of Search If the tree is complete binary or nearly complete, searching through 100,000 numbers will require a maximum of 20 comparisons. Or in general, approximately  log 2 (n) . Compare this with a linked list of 100,000 numbers. The comparisons required could be a maximum of  n .  http://ecomputernotes.com
Binary Search Tree A binary tree with the property that items in the left subtree are smaller than the root and items are larger or equal in the right subtree is called a  binary search tree  (BST). The tree we built for searching for duplicate numbers was a binary search tree. BST and its variations play an important role in searching algorithms. http://ecomputernotes.com
Traversing a Binary Tree Suppose we have a binary tree, ordered (BST) or unordered. We want to print all the values stored in the nodes of the tree. In what order should we print them? http://ecomputernotes.com
Traversing a Binary Tree Ways to print a 3 node tree: 14 15 4 (4, 14, 15), (4,15,14) (14,4,15), (14,15,4) (15,4,14), (15,14,4) http://ecomputernotes.com
Traversing a Binary Tree In case of the general binary tree: node (L,N,R), (L,R,N) (N,L,R), (N,R,L) (R,L,N), (R,N,L) left subtree right subtree L N R http://ecomputernotes.com
Traversing a Binary Tree Three common ways node Preorder: (N,L,R) Inorder: (L,N,R) Postorder: (L,R,N) left subtree right subtree L N R http://ecomputernotes.com
Traversing a Binary Tree void preorder(TreeNode<int>* treeNode) { if( treeNode != NULL )  { cout << *(treeNode->getInfo())<<&quot; &quot;; preorder(treeNode->getLeft()); preorder(treeNode->getRight()); } } http://ecomputernotes.com
Traversing a Binary Tree void inorder(TreeNode<int>* treeNode) { if( treeNode != NULL )  { inorder(treeNode->getLeft()); cout << *(treeNode->getInfo())<<&quot; &quot;; inorder(treeNode->getRight()); } } http://ecomputernotes.com
Traversing a Binary Tree void postorder(TreeNode<int>* treeNode) { if( treeNode != NULL )  { postorder(treeNode->getLeft()); postorder(treeNode->getRight()); cout << *(treeNode->getInfo())<<&quot; &quot;; } } http://ecomputernotes.com
Traversing a Binary Tree cout << &quot;inorder: &quot;; preorder( root); cout << &quot;inorder: &quot;; inorder( root ); cout << &quot;postorder: &quot;; postorder( root ); http://ecomputernotes.com
Traversing a Binary Tree Preorder: 14 4 3 9 7 5 15 18 16 17 20 14 15 4 9 7 18 3 5 16 20 17 http://ecomputernotes.com
Traversing a Binary Tree Inorder: 3 4 5 7 9 14 15 16 17 18 20 14 15 4 9 7 18 3 5 16 20 17 http://ecomputernotes.com
Traversing a Binary Tree Postorder: 3 5 7 9 4 17 16 20 18 15 14 14 15 4 9 7 18 3 5 16 20 17 http://ecomputernotes.com
Recursive Call Recall that a stack is used during function calls. The caller function places the arguments on the stack and passes control to the called function. Local variables are allocated storage on the call stack. Calling a function itself makes no difference as far as the call stack is concerned.  http://ecomputernotes.com
Stack Layout during a call Here is stack layout when function F calls function F (recursively): Parameters(F) Local variables(F) Return address(F) Parameters(F) Parameters(F) Local variables(F) Return address(F) Parameters(F) Local variables(F) Return address(F) Parameters(F) Local variables(F) Return address(F) During execution of F After call At point of call sp sp sp http://ecomputernotes.com

More Related Content

PPTX
Binomial Heaps and Fibonacci Heaps
DOCX
Chapter 4
TXT
pROgRAN C++ ApLiKaSI Binery tree
PPT
Trees - Data structures in C/Java
PPTX
Binary Tree in Data Structure
PDF
D3 data, user, interaction
PPT
Binary Search Tree and AVL
Binomial Heaps and Fibonacci Heaps
Chapter 4
pROgRAN C++ ApLiKaSI Binery tree
Trees - Data structures in C/Java
Binary Tree in Data Structure
D3 data, user, interaction
Binary Search Tree and AVL

What's hot (20)

PPTX
Binary Search Tree
PPTX
B trees
PPTX
Binary search tree
PPT
Cinterviews Binarysearch Tree
PDF
Binary tree
PDF
Welcome to International Journal of Engineering Research and Development (IJERD)
PPTX
Binary Search Tree in Data Structure
PDF
Nach os assignment_2_teorica
PDF
SAS and R Code for Basic Statistics
PPTX
R seminar dplyr package
PDF
Manipulating Data using DPLYR in R Studio
PPTX
Querying and Merging Heterogeneous Data by Approximate Joins on Higher-Order ...
PDF
Jamal ppt b+tree dbms
PPTX
PDF
Data manipulation with dplyr
PDF
Introduction to data.table in R
PPTX
Lecture 7 bst
PDF
CSS: The Boring Bits
PPT
Trees
PPTX
R brownbag seminar 2.3
Binary Search Tree
B trees
Binary search tree
Cinterviews Binarysearch Tree
Binary tree
Welcome to International Journal of Engineering Research and Development (IJERD)
Binary Search Tree in Data Structure
Nach os assignment_2_teorica
SAS and R Code for Basic Statistics
R seminar dplyr package
Manipulating Data using DPLYR in R Studio
Querying and Merging Heterogeneous Data by Approximate Joins on Higher-Order ...
Jamal ppt b+tree dbms
Data manipulation with dplyr
Introduction to data.table in R
Lecture 7 bst
CSS: The Boring Bits
Trees
R brownbag seminar 2.3
Ad

Viewers also liked (20)

PPT
computer notes - Data Structures - 25
PPT
computer notes - Data Structures - 4
PPT
computer notes - Data Structures - 14
PPT
computer notes - Data Structures - 6
PPT
computer notes - Data Structures - 21
PPT
computer notes - Data Structures - 27
PPT
computer notes - Data Structures - 36
PPT
computer notes - Data Structures - 37
PPT
computer notes - Data Structures - 1
PPT
computer notes - Data Structures - 18
PPT
computer notes - Data Structures - 7
PPT
computer notes - Data Structures - 17
PPT
computer notes - Data Structures - 5
PPT
computer notes - Data Structures - 22
PPT
computer notes - Data Structures - 35
PPT
computer notes - Data Structures - 15
PPT
computer notes - Data Structures - 39
PPT
computer notes - Data Structures - 34
PPT
computer notes - Data Structures - 23
PPT
computer notes - Data Structures - 16
computer notes - Data Structures - 25
computer notes - Data Structures - 4
computer notes - Data Structures - 14
computer notes - Data Structures - 6
computer notes - Data Structures - 21
computer notes - Data Structures - 27
computer notes - Data Structures - 36
computer notes - Data Structures - 37
computer notes - Data Structures - 1
computer notes - Data Structures - 18
computer notes - Data Structures - 7
computer notes - Data Structures - 17
computer notes - Data Structures - 5
computer notes - Data Structures - 22
computer notes - Data Structures - 35
computer notes - Data Structures - 15
computer notes - Data Structures - 39
computer notes - Data Structures - 34
computer notes - Data Structures - 23
computer notes - Data Structures - 16
Ad

Similar to computer notes - Data Structures - 13 (20)

PPT
Data structures final lecture 1
DOC
e computer notes - Binary search tree
PPTX
Week 8 (trees)
PDF
computer notes - Binary search tree
PDF
1. Add a breadth-first (level-order) traversal function to the binar.pdf
PPT
6_1 (1).ppt
PPT
Algorithm and Data Structure - Binary Trees
PPT
part4-trees.ppt
PPTX
Binary tree
PPT
computer notes - Data Structures - 12
PDF
Binary Tree - Algorithms
PDF
MAINCPP include ltiostreamgt include ltstringgt u.pdf
PPTX
Data structures and Algorithm analysis_Lecture4.pptx
PPT
Lecture10
PPTX
UNIT 2 TREES & GRAPH COMPLETE NOTES OF DATA STRUCTURE
PPTX
learn tree, linked list, queue, stack, and other algo
PDF
Binary Trees
PPT
FRbsbsvvsvsvbshgsgsvzvsvvsvsvsvsvsvvev.ppt
PPTX
BST.pptx this is Good for data structure
PPTX
BST.pptx this isp used for learning binary search trees
Data structures final lecture 1
e computer notes - Binary search tree
Week 8 (trees)
computer notes - Binary search tree
1. Add a breadth-first (level-order) traversal function to the binar.pdf
6_1 (1).ppt
Algorithm and Data Structure - Binary Trees
part4-trees.ppt
Binary tree
computer notes - Data Structures - 12
Binary Tree - Algorithms
MAINCPP include ltiostreamgt include ltstringgt u.pdf
Data structures and Algorithm analysis_Lecture4.pptx
Lecture10
UNIT 2 TREES & GRAPH COMPLETE NOTES OF DATA STRUCTURE
learn tree, linked list, queue, stack, and other algo
Binary Trees
FRbsbsvvsvsvbshgsgsvzvsvvsvsvsvsvsvvev.ppt
BST.pptx this is Good for data structure
BST.pptx this isp used for learning binary search trees

More from ecomputernotes (20)

PPT
computer notes - Data Structures - 30
PPT
computer notes - Data Structures - 11
PPT
computer notes - Data Structures - 20
DOC
Computer notes - Including Constraints
DOC
Computer notes - Date time Functions
DOC
Computer notes - Subqueries
DOC
Computer notes - Other Database Objects
PPT
computer notes - Data Structures - 28
PPT
computer notes - Data Structures - 19
PPT
computer notes - Data Structures - 31
DOC
Computer notes - Advanced Subqueries
DOC
Computer notes - Aggregating Data Using Group Functions
DOC
Computer notes - Enhancements to the GROUP BY Clause
DOC
Computer notes - Manipulating Data
DOC
Computer notes - Writing Basic SQL SELECT Statements
PPT
computer notes - Data Structures - 10
DOC
Computer notes - Controlling User Access
DOC
Computer notes - Using SET Operator
PPT
computer notes - Data Structures - 29
PPT
computer notes - Data Structures - 38
computer notes - Data Structures - 30
computer notes - Data Structures - 11
computer notes - Data Structures - 20
Computer notes - Including Constraints
Computer notes - Date time Functions
Computer notes - Subqueries
Computer notes - Other Database Objects
computer notes - Data Structures - 28
computer notes - Data Structures - 19
computer notes - Data Structures - 31
Computer notes - Advanced Subqueries
Computer notes - Aggregating Data Using Group Functions
Computer notes - Enhancements to the GROUP BY Clause
Computer notes - Manipulating Data
Computer notes - Writing Basic SQL SELECT Statements
computer notes - Data Structures - 10
Computer notes - Controlling User Access
Computer notes - Using SET Operator
computer notes - Data Structures - 29
computer notes - Data Structures - 38

Recently uploaded (20)

PDF
KodekX | Application Modernization Development
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
Machine learning based COVID-19 study performance prediction
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Electronic commerce courselecture one. Pdf
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
Review of recent advances in non-invasive hemoglobin estimation
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PPTX
MYSQL Presentation for SQL database connectivity
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
Network Security Unit 5.pdf for BCA BBA.
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PPTX
Programs and apps: productivity, graphics, security and other tools
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
DOCX
The AUB Centre for AI in Media Proposal.docx
KodekX | Application Modernization Development
Encapsulation_ Review paper, used for researhc scholars
Machine learning based COVID-19 study performance prediction
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
“AI and Expert System Decision Support & Business Intelligence Systems”
Mobile App Security Testing_ A Comprehensive Guide.pdf
Dropbox Q2 2025 Financial Results & Investor Presentation
Electronic commerce courselecture one. Pdf
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Review of recent advances in non-invasive hemoglobin estimation
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Understanding_Digital_Forensics_Presentation.pptx
Reach Out and Touch Someone: Haptics and Empathic Computing
MYSQL Presentation for SQL database connectivity
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Network Security Unit 5.pdf for BCA BBA.
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
Programs and apps: productivity, graphics, security and other tools
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
The AUB Centre for AI in Media Proposal.docx

computer notes - Data Structures - 13

  • 1. Class No.13 Data Structures http://ecomputernotes.com
  • 2. Trace of insert 17, 9, 14, 5 14 15 4 9 7 18 3 5 16 20 17 p p->setRight( node ); node http://ecomputernotes.com
  • 3. Cost of Search Given that a binary tree is level d deep. How long does it take to find out whether a number is already present? Consider the insert(17) in the example tree. Each time around the while loop, we did one comparison. After the comparison, we moved a level down . http://ecomputernotes.com
  • 4. Cost of Search With the binary tree in place, we can write a routine find(x) that returns true if the number x is present in the tree, false otherwise. How many comparison are needed to find out if x is present in the tree? We do one comparison at each level of the tree until either x is found or q becomes NULL. http://ecomputernotes.com
  • 5. Cost of Search If the binary tree is built out of n numbers, how many comparisons are needed to find out if a number x is in the tree? Recall that the depth of the complete binary tree built using ‘ n ’ nodes will be log 2 (n+1) – 1. For example, for n=100,000, log 2 (100001) is less than 20; the tree would be 20 levels deep. http://ecomputernotes.com
  • 6. Cost of Search If the tree is complete binary or nearly complete, searching through 100,000 numbers will require a maximum of 20 comparisons. Or in general, approximately log 2 (n) . Compare this with a linked list of 100,000 numbers. The comparisons required could be a maximum of n . http://ecomputernotes.com
  • 7. Binary Search Tree A binary tree with the property that items in the left subtree are smaller than the root and items are larger or equal in the right subtree is called a binary search tree (BST). The tree we built for searching for duplicate numbers was a binary search tree. BST and its variations play an important role in searching algorithms. http://ecomputernotes.com
  • 8. Traversing a Binary Tree Suppose we have a binary tree, ordered (BST) or unordered. We want to print all the values stored in the nodes of the tree. In what order should we print them? http://ecomputernotes.com
  • 9. Traversing a Binary Tree Ways to print a 3 node tree: 14 15 4 (4, 14, 15), (4,15,14) (14,4,15), (14,15,4) (15,4,14), (15,14,4) http://ecomputernotes.com
  • 10. Traversing a Binary Tree In case of the general binary tree: node (L,N,R), (L,R,N) (N,L,R), (N,R,L) (R,L,N), (R,N,L) left subtree right subtree L N R http://ecomputernotes.com
  • 11. Traversing a Binary Tree Three common ways node Preorder: (N,L,R) Inorder: (L,N,R) Postorder: (L,R,N) left subtree right subtree L N R http://ecomputernotes.com
  • 12. Traversing a Binary Tree void preorder(TreeNode<int>* treeNode) { if( treeNode != NULL ) { cout << *(treeNode->getInfo())<<&quot; &quot;; preorder(treeNode->getLeft()); preorder(treeNode->getRight()); } } http://ecomputernotes.com
  • 13. Traversing a Binary Tree void inorder(TreeNode<int>* treeNode) { if( treeNode != NULL ) { inorder(treeNode->getLeft()); cout << *(treeNode->getInfo())<<&quot; &quot;; inorder(treeNode->getRight()); } } http://ecomputernotes.com
  • 14. Traversing a Binary Tree void postorder(TreeNode<int>* treeNode) { if( treeNode != NULL ) { postorder(treeNode->getLeft()); postorder(treeNode->getRight()); cout << *(treeNode->getInfo())<<&quot; &quot;; } } http://ecomputernotes.com
  • 15. Traversing a Binary Tree cout << &quot;inorder: &quot;; preorder( root); cout << &quot;inorder: &quot;; inorder( root ); cout << &quot;postorder: &quot;; postorder( root ); http://ecomputernotes.com
  • 16. Traversing a Binary Tree Preorder: 14 4 3 9 7 5 15 18 16 17 20 14 15 4 9 7 18 3 5 16 20 17 http://ecomputernotes.com
  • 17. Traversing a Binary Tree Inorder: 3 4 5 7 9 14 15 16 17 18 20 14 15 4 9 7 18 3 5 16 20 17 http://ecomputernotes.com
  • 18. Traversing a Binary Tree Postorder: 3 5 7 9 4 17 16 20 18 15 14 14 15 4 9 7 18 3 5 16 20 17 http://ecomputernotes.com
  • 19. Recursive Call Recall that a stack is used during function calls. The caller function places the arguments on the stack and passes control to the called function. Local variables are allocated storage on the call stack. Calling a function itself makes no difference as far as the call stack is concerned. http://ecomputernotes.com
  • 20. Stack Layout during a call Here is stack layout when function F calls function F (recursively): Parameters(F) Local variables(F) Return address(F) Parameters(F) Parameters(F) Local variables(F) Return address(F) Parameters(F) Local variables(F) Return address(F) Parameters(F) Local variables(F) Return address(F) During execution of F After call At point of call sp sp sp http://ecomputernotes.com

Editor's Notes

  • #3: End of lecture 12
  • #21: End of lecture 13.